package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.enums.StatusEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysMember;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.domain.dto.HomePageReportDTO;
import com.ruoyi.system.domain.vo.HomePageIndexVo;
import com.ruoyi.system.domain.vo.HomePageReportVo;
import com.ruoyi.system.mapper.SysMemberMapper;
import com.ruoyi.system.mapper.SysOrderMapper;
import com.ruoyi.system.service.IHomePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class HomePageServiceImpl implements IHomePageService {

    @Autowired
    private SysMemberMapper sysMemberMapper;

    @Autowired
    private SysOrderMapper sysOrderMapper;

    @Override
    public HomePageIndexVo getHomePageIndex() {

        //可以使用redis优化
        HomePageIndexVo homePageIndexVo = new HomePageIndexVo();
        List<SysMember> memberList = sysMemberMapper.selectList(new LambdaQueryWrapper<>(new SysMember()).eq(SysMember::getStatus, StatusEnum.ENABLE.getCode()));
        homePageIndexVo.setMemberCount(memberList.size());
        homePageIndexVo.setStudentCount(memberList.stream().filter(member -> member.getStudentFlag() == 1).count());

        List<SysOrder> orderList = sysOrderMapper.selectList(new LambdaQueryWrapper<>(new SysOrder()).eq(SysOrder::getDeleteFlag, StatusEnum.NORMAL.getCode()));
        homePageIndexVo.setTotalAmount(orderList.stream().map(SysOrder::getPayAmount).reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
        homePageIndexVo.setTotalAmountToday(orderList.stream().filter(order -> order.getDealTime().after(DateUtils.getDayStartDate(new Date()))).map(SysOrder::getPayAmount).reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
        return homePageIndexVo;
    }

    @Override
    public List<HomePageReportVo> getHomePageReport(HomePageReportDTO dto) {

        List<HomePageReportVo> list = new ArrayList<>();
        LambdaQueryWrapper<SysMember> queryWrapper = new LambdaQueryWrapper<>();
        if (dto.getStartDate() != null && dto.getEndDate() != null){
            queryWrapper.between(SysMember::getCreateTime, dto.getStartDate(), dto.getEndDate());
        }
        if (dto.getLevel1Id() != null){
            queryWrapper.eq(SysMember::getLevel1Id, dto.getLevel1Id());
        }
        if (dto.getLevel2Id() != null){
            queryWrapper.eq(SysMember::getLevel2Id, dto.getLevel2Id());
        }
        List<SysMember> memberList = sysMemberMapper.selectList(queryWrapper);

        HomePageReportVo memberCount = new HomePageReportVo();
        memberCount.setName("发展会员数");

        Map<Date, Long> collect = memberList.stream().collect(Collectors.groupingBy(obj -> DateUtils.getMonthStartDate(obj.getCreateTime()), Collectors.counting()));
        memberCount.setData(new ArrayList<>(collect.values()));

        list.add(memberCount);

        HomePageReportVo studentCount = new HomePageReportVo();
        studentCount.setName("发展学员数");

        Map<Date, Long> studentCollect = memberList.stream().filter(member -> member.getStudentFlag() == 1).collect(Collectors.groupingBy(obj -> DateUtils.getMonthStartDate(obj.getCreateTime()), Collectors.counting()));
        studentCount.setData(new ArrayList<>(studentCollect.values()));
        list.add(studentCount);

        HomePageReportVo totalAmount = new HomePageReportVo();
        totalAmount.setName("成交总金额（万元）");
        LambdaQueryWrapper<SysOrder> queryWrapper1 = new LambdaQueryWrapper<>();
        if (dto.getStartDate() != null && dto.getEndDate() != null){
            queryWrapper1.between(SysOrder::getCreateTime, dto.getStartDate(), dto.getEndDate());
        }
        if (dto.getLevel1Id() != null){
            queryWrapper1.eq(SysOrder::getLevel1Id, dto.getLevel1Id());
        }
        if (dto.getLevel2Id() != null){
            queryWrapper1.eq(SysOrder::getLevel2Id, dto.getLevel2Id());
        }
        List<SysOrder> orderList = sysOrderMapper.selectList(queryWrapper1);

        Map<Date, BigDecimal> collect1 = orderList.stream().collect(Collectors.groupingBy(order -> DateUtils.getMonthStartDate(order.getDealTime()), Collectors.reducing(BigDecimal.ZERO, SysOrder::getPayAmount, BigDecimal::add))).entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().divide(new BigDecimal("10000"), 2, BigDecimal.ROUND_HALF_UP)));
        totalAmount.setData(new ArrayList<>(collect1.values()));
        list.add(totalAmount);
        return list;
    }
}
